<?php

/**
 * Controller for actions (in visitor area) of account module
 *  
 * @version 2012-10-10
 * @package account
 * @subpackage controller
 * @category Account_Controller_Visitor
 */

 
class Account_Controller_Visitor extends Controller
{

	/** 
	* Index
	* Default function that will be used upon module start-up.
    *
    * @version 2012-10-14
    * @param Request $req
    * @return view
    * @access public
    */
    function processIndex($req)
    {
        return $this->processLogin($req);
    }
    
    /**
	* Login
    * This function displays the log-in form.
    *
    * @version 2012-12-10
    * @param Request $req
    * @return View (login page)
    * @access public
    */
    public function processLogin($req)
    {
        if($this->_accountId)
            $req->forward(_cfg('url'));
        $view = $this->getView("visitor_login");
        $data = $req->getArray('data');
   
        $view->setVar('data', $data);
        return $view;
    }  


    /**
    * DoLogin
    * This function checks login and password in the database. If login and password coincide with those recorded in the database, a redirect to the member area is performed.
    *
    * @version 2012-12-10
    * @param Request $req
    * @return View (login page)
    * @access public
    */
    public function processDoLogin($req)
    {
        $data = $req->getArray('data');
        $dbAccounts = Db::getModel("member_accounts");
        $result = $dbAccounts->findBy(array("login_key"=>"='"._encoder($data['login'])."'","utype"=>"='member'"),array(),0,1);
        if(empty($data['login']) or empty($data['password'])){
            return $this->processLogin($req);
        }
        elseif($result[0]){
           if($result[0]['blocked'] <= 0){
            $this->addErrors(_fmt('account.blocked')); 
            return $this->processLogin($req);
           } elseif(_pass($data['password']) == $result[0]['password_key']){
               $_SESSION[$result[0]['utype']] = array(
                    "id" => $result[0]['uuid']
                 );
               $dbAccounts->update(array('last_visit'=> _dformat(time(),_cfg('format.datetime.sql')),'blocked'=>3), $result[0]['uuid']);
               $req->forward(_cfg('url.'.$rows[0]['utype']));
               _d($_SESSION);
           }
           else {
                $dbAccounts->update(array("blocked"=>($result[0]['blocked'] - 1)),$result[0]['uuid']);
                $this->addErrors(_fmt('account.login.error')); 
                return $this->processLogin($req);
           }
        }else{
            $this->addErrors(_fmt('account.login.error')); 
            return $this->processLogin($req);
        }
        exit;
    }

    /**
    * YouBlocked
    * This function displays the log-in form.
    *
    * @version 2012-10-14
    * @param Request $req
    * @return View (login page)
    * @access public
    */
    public function processYouBlocked($req)
    {
        $view = $this->getView("visitor_you_blocked");
        return $view;
    } 

    /**
    * GetUnblockedEmail
    * This function send email unblocked link.
    *
    * @version 2012-10-14
    * @param Request $req
    * @return View (login page)
    * @access public
    */
    public function processGetUnblockedEmail($req)
    {
        $fields['data[email]'] = array('title'=>'Email', 'def' => 'email');
        $val = new Validator($fields); 
  
        if(!$val->isValid($req->getArgs())){
            $this->addErrors($val->getErrors()); 
            return $this->processYouBlocked($req);  
        }else{
            $req->forward("/index.php",_fmt('email.umblocked.link'));
        }
        exit;
    } 
}
